package dse.messages.multiversion;

import java.util.*;
import dse.*;
import dse.messages.*;
import dse.messages.singleversion.ShareDownloader;

public class ShareUpdateV4MessageHandler extends MessageSender implements MessageHandler {
	private DataObject dobj;
	private Version version;
	private int size;
	private int shareIndex;
	private boolean tranx;
	private ShareDownloader downloader;
	
	public ShareUpdateV4MessageHandler (Experiment experiment, DataObject dobj, int shareIndex,
			Version version, boolean tranx, ShareDownloader downloader)
	{
		super(experiment);
		
		this.dobj = dobj;
		this.version = version;
		this.size = experiment.modules().nodeEventScheduler().getEventsPerPeriod()+1;
		if(size == 0)
			size = 1;
		this.shareIndex = shareIndex;
		this.tranx = tranx;
		this.downloader = downloader;
	}
	
	@Override
	public boolean acceptMessage(Message message) {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public void messageInterupted(ExperimentRecorder recorder, Message message,
			double time) {
		// TODO Auto-generated method stub
		recorder.debug(this, "msgIntrpt: " + message, time);
		Message m = super.createShareUpdateV4ResponseMessage(message.to(), message.from(), 
				dobj, shareIndex, tranx, false, message.purpose(), downloader);

		recorder.debug(this, "new msg: " + m, time);
		experiment.messageQueue().enqueueMessage(m, time);
	}

	@Override
	public void messageReceived(ExperimentRecorder recorder, Message message,
			double time) {
		// TODO Auto-generated method stub
		recorder.debug(this, "msgRcvd: " + message, time);
		if (message.messageType() != MessageType.ShareUpdateV4) {
			recorder.warn(
					"ShareUpdateV4MessageHandler recieved " + 
					message.messageType() + " message."
					);
			return;
		}			
		// What do we know about the share?
		Node receiver = message.to();
		
		// Does the receiver have the share?
		DataShare share = receiver.getDataShare(dobj);
		if (share != null)
		{
			// Then we need to send a new message BACK (thats why to and from
			// are reversed!
			boolean contain = false;
			for(Version ver : share.getVersionsV2()){
				if(ver.compareTo(version) == 0)
					contain = true;
			}
			if(!contain)
				share.addNewVersion(version);	
			share.removeOldVersion();
			
			if(tranx)
			{
				Message m = super.createShareUpdateV4ResponseMessage(receiver, message.from(), 
						dobj, shareIndex, tranx, true, message.purpose(), downloader);
	
				recorder.debug(this, "new msg: " + m, time);
				experiment.messageQueue().enqueueMessage(m, time);
			}
			
			if(!contain)
			{
				LinkedList<Cluster> clusterRing = MessageUtil.getClustersRing(experiment.network(), 
						experiment.network().lookupCluster(receiver.clusterId()), dobj, experiment.modules().assigner());
				if(clusterRing.size() == 0)
					return;
				Node to = experiment.modules().assigner().assign(dobj, shareIndex, clusterRing.getFirst());
				Message m = super.createShareUpdateV4Message(receiver, to, dobj, shareIndex, version, false, message.purpose(), downloader);
	
				recorder.debug(this, "new msg: " + m, time);
				experiment.messageQueue().enqueueMessage(m, time);
			}
		}
		else
		{
			this.messageInterupted(recorder, message, time);
		}
	}
}
